package cn.tom.entity;


import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import sun.util.resources.cldr.ig.CalendarData_ig_NG;

import java.io.Serializable;
import java.util.*;
import java.util.Collection;

@Data
public class XUser implements UserDetails {
    private Integer id;         //主键
    private String account;     //账号
    private String password;    //密码
    private String phone;       //电话
    private String nickname;    //姓名
    private Integer vip;         // 0-普通用户 1-会员
    private String addr;        //地址
    private String img;         //头像
    private Integer closed;    //是否关闭  冻结  0 no冻结  1  yes冻结
    private String username;   // 用于保存登录账号  账号/密码
    private List<String> roles;

    public XUser() {
            this.id = 0;
            this.account = "";
            this.password = "";
            this.phone = "";
            this.nickname = "";
            this.vip = 0;
            this.addr = "";
            this.img = "";
            this.closed = 0;
            roles = null;
    }

    @Override
    @JsonIgnore
    public Collection<? extends GrantedAuthority> getAuthorities() {
        System.out.println(1);
        if (this.roles != null){
            List<SimpleGrantedAuthority> authorities = new ArrayList<>();
            for (String role : this.roles) {
                authorities.add(new SimpleGrantedAuthority(role));
            }
            return authorities;
        }
        return Collections.emptyList();
    }

    @Override
    public String getPassword() {
        return this.password;
    }

    @Override
    public String getUsername() {
        return this.account;
    }

    @Override
    @JsonIgnore
    public boolean isAccountNonExpired() {
        return true;
    }
    //该用户已冻结
    @Override
    @JsonIgnore
    public boolean isAccountNonLocked() {
        if(this.getClosed() == 1) {
            return false;
        }
        return true;
    }
    //false 是用户凭证已过期
    @Override
    @JsonIgnore
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    @JsonIgnore
    public boolean isEnabled() {
        return true;
    }
}
